<template>
  <div class="tree-item" style="width: 100%">
    <span class="i1">{{ item.name }}</span>
    <span class="i2">{{ item.manager }}</span>
    <el-dropdown class="i3" @command="commandEvent">
      <span>
        <span>操作</span>
        <i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="!item.isTop" command="look"
            >查看部门</el-dropdown-item
          >
          <el-dropdown-item v-if="!item.isTop" command="del"
            >删除部门</el-dropdown-item
          >
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDle } from "@/api/departments.js";
export default {
  props: ["item"],
  methods: {
    commandEvent(command) {
      // if (command === 'add') {
      //   console.log('添加子部门')
      //   this.$bus.$emit('show', true, this.item)
      // }
      // else if (command === 'del'){

      // }

      switch (command) {
        case "add":
          this.$bus.$emit("show", true, this.item);
          break;
        case "del":
          this.$$confirm("您确定删除该部门吗？", "提示")
            .then(async () => {
              await companyDepartmentDle(this.item.id);
              this.$message.success("删除部门成功");
              this.$emit("getData");
            })
            .catch(() => {});
          break;
        // 点击查看传入edit
        case "look":
          this.$bus.$emit("show", true, this.item, "edit");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.tree-item {
  display: flex;
  align-items: center;
  .i1 {
    flex: 1;
  }
  .i2 {
    width: 150px;
    text-align: right;
  }
  .i3 {
    width: 150px;
    text-align: right;
  }
}
</style>
